/**
 * Created by 13296 on 2018/7/11.
 */
//查询节点加载
let searchInitVideo=function (video,node) {
    let s = '<div class="col-md-3 resent-grid recommended-grid">'+
        '   <div class="resent-grid-img recommended-grid-img">'+
        '       <a href="'+getRootPath_web()+'/video/single?video_id='+video.video_id+'"><img src="'+video.image_url+'" alt="" /></a>'+
        '       <div class="time small-time">'+
        '           <p>'+video.total_time+'</p>'+
        '       </div>'+
        '       <div class="clck small-clck">'+
        '           <span class="glyphicon glyphicon-time" aria-hidden="true"></span>'+
        '       </div>'+
        '   </div>'+
        '   <div class="resent-grid-info recommended-grid-info video-info-grid">'+
        '       <h5><a href="'+getRootPath_web()+'/video/single?video_id='+video.video_id+'" class="title">'+video.title+'</a></h5>'+
        '       <ul>'+
        '           <li><p class="author author-info"><a href="#" class="author">'+video.account+'</a></p></li>'+
        '           <li class="right-list"><p class="views views-info">'+video.views+' views</p></li>'+
        '       </ul>'+
        '   </div>'+
        '</div>';
    if(node != "#"){
        $(node).append(s);
    }
    return s;
};
//根据视频类型展示视频
let search_display=function (value,video_type,pn){
    // console.log("display"+pn.toString())
    let url = "/video/search";
    let method = "GET";
    let data = {
        search:value,
        video_type:video_type,
        pageNum:pn,
    };
    ajax_base(data, url, method, "Json").then(function (data) {
            if (data.success) {
                let videos = data.obj;
                console.log(videos);
                let node = '#';
                let s ='';
                if( videos.length !=0){
                    for(var i = 0; i < videos.length; i++) {
                        if(i<4){
                            s += searchInitVideo(videos[i],node);
                            if(i==3 || i==(videos.length-1)){
                                s += '<div class="clearfix"> </div>';
                                let insert_str = '<div class="recommended-grids">'+s+'</div>';
                                // console.log(insert_str);
                                $(".recommended").append(insert_str);
                                s = '';
                            }
                        }else{
                            s += searchInitVideo(videos[i],node);
                            if(i==7 || i==(videos.length-1)){
                                s += '<div class="clearfix"> </div>';
                                let insert_str = '<div class="recommended-grids">'+s+'</div>';
                                // console.log(insert_str);
                                $(".recommended").append(insert_str);
                                s = '';
                            }
                        }
                    }
                }else{
                    my_alert(200,"已经加载全部！");
                }
            }
        }
    );
};

//全局变量
let search_video_type = "0";
let search_pn = 2;
let flag = 0;
//滚动条触底刷新
let searchScrollBottom =function(){
    var minAwayBtm = 2;  // 距离页面底部的最小距离
    $(window).scroll(function() {
        if(scroll_flag ==1){//滚轮事件有效
            var awayBtm = $(document).height() - $(window).scrollTop() - $(window).height();
            // console.log('当前距离页面底部：' + awayBtm + 'px');
            if (awayBtm <= minAwayBtm ) {
                if(flag!=0){
                    console.log('触发了'+search_pn.toString());
                    // let video_type = "0";
                    let value = $("#search_value").val();
                    search_display(value,search_video_type,search_pn);
                    search_pn ++;
                }else{
                    flag =1;
                }
            }
        }
    });
};

let video_type_mapper = function (video_type) {
    switch (video_type){
        case "全部":
            return "0";
        case "News":
            return "news";
        case "TV Shows":
            return "tv_shows";
        case "Movies":
            return "movies";
        case "Sports":
            return "sports";
        case "Songs":
            return "songs";
        default:
            return "0";
    }
};

//加载搜索类型导航
let initNav = function () {
    let s =  '<div class="mod_nav_list cf">'+
            '   <ul class="nav_list">'+
            '       <li class="list_item">'+
            '           <a class="item" href="javascript:;" style="color: #ff7800;">全部</a>'+
            '       </li>'+
            '       <li class="list_item">'+
            '           <a class="item" href="javascript:;">News</a>'+
            '       </li>'+
            '       <li class="list_item current">'+
            '           <a class="item" href="javascript:;">TV Shows</a>'+
            '       </li>'+
            '       <li class="list_item">'+
            '           <a class="item" href="javascript:;">Movies</a>'+
            '       </li>'+
            '       <li class="list_item">'+
            '           <a class="item" href="javascript:;">Sports</a>'+
            '       </li>'+
            '       <li class="list_item">'+
            '           <a class="item" href="javascript:;">Songs</a>'+
            '       </li>'+
            '       <span class="nav_span"></span>'+
            '   </ul>'+
            '</div>';
    $(".main").prepend(s);

    $(".nav_list>.list_item").hover(function() {
        var x = $(this).offset().left-295; //计算li的位置
        $(".nav_span").css("marginLeft", x);
        $(".nav_span").css("width",$(this).width());
        $(this).children("a").css("color","#ff7800");
        // console.log($(this).width());

        //刷新界面
        $(".main-grids").children().remove();
        $(".main-grids").append('<div class="recommended"></div>');
        search_pn = 2;
        // flag = 0;//标志
        search_video_type = video_type_mapper($(this).children("a").text());
        let pageNum = 1;
        let search_value = $("#search_value").val();
        console.log(search_video_type);
        console.log(pageNum);
        console.log(search_value);
        search_display(search_value,search_video_type,pageNum);

    }, function() {
        // $(".nav_span").css("marginLeft", "0");
        $(this).children("a").css("color","white");
    })

};

//加载主页面
let searchRun=function () {

    //搜索
    $("#search_submit").click(function () {
        scroll_flag =1;//启动滚轮事件
        search_pn = 2;
        console.log("点击了！");
        //删除子节点
        $(".mod_nav_list").remove();
        $(".main-grids").children().remove();
        $(".main-grids").append('<div class="recommended"></div>');
        //加载导航
        initNav();

        search_video_type = "0";
        let pageNum = 1;
        let search_value = $("#search_value").val();
        // console.log(video_type);
        // console.log(pageNum);
        // console.log(search_value);
        search_display(search_value,search_video_type,pageNum);
        return false;
    });

    //滚动条事件
    searchScrollBottom();
};searchRun();